// src/stores/project/menu.ts
import { defineStore } from 'pinia';
import { usePermissionStore } from '../permission';
import { useUserStore } from '../user';

// 菜单项名称表
export const useMenuStore = defineStore('menu', {
  state: () => ({

    // 前台菜单（对所有用户默认可见）
    frontMenuNames: {
      '/front': '首页',
      '/announcement': '公告信息',
      '/news': '新闻资讯',
    } as Record<string, string>,

    // 管理菜单（需要权限控制）
    manageMenuNames: {
      '/announcement-manage': '公告管理',
      '/news-manage': '新闻管理',
      '/department-manage': '部门管理',
      '/vehicle-manage': '车辆管理',
      '/employee-manage': '员工管理',
      '/material-type-manage': '物资类型管理',
      '/material-manage': '物资管理',
      '/material-inbound-manage': '物资入库管理',
      '/material-outbound-manage': '物资出库管理',
      '/feedback-manage': '留言反馈管理',
      '/application-manage': '物资申请管理',
      '/transportation-manage': '运输管理',
      '/permission-manage': '权限管理',
    } as Record<string, string>,

  }),
  getters: {

    // 获取前台菜单（根据权限过滤管理菜单）
    getFrontMenuNames: (state) => {
      const permissionStore = usePermissionStore();
      const userStore = useUserStore();

      if (!userStore.user || !userStore.user.id) {
        return { ...state.frontMenuNames };
      }

      try {
        const userPermissions = permissionStore.permissions[userStore.user.id] || [];
        const filteredManageMenus = Object.fromEntries(
          Object.entries(state.manageMenuNames).filter(([path]) =>
            userPermissions.includes(path)
          )
        );

        return { ...state.frontMenuNames, ...filteredManageMenus };
      } catch (error) {
        console.error('MenuStore: 获取前台菜单失败:', error);
        return { ...state.frontMenuNames };
      }
    },

    // 获取后台菜单（管理员专用，包含所有管理菜单）
    getAdminMenuNames: (state) => {
      return state.manageMenuNames;
    },

    getMenuName: (state) => (path: string) => {
      return (
        state.frontMenuNames[path] ||
        state.manageMenuNames[path] ||
        path
      );
    },
  },
});